package com.docparser.processor;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 处理上标元素。
 */
public class SuperscriptProcessor extends BaseProcessor {
    /**
     * 处理上标元素。
     */
    public static void process(Element supElement, StringBuilder formula, ProcessorCallback callback) {
        if (supElement == null) return;
        
        StringBuilder base = new StringBuilder();
        StringBuilder superscript = new StringBuilder();
        
        // 记录处理状态
        ProcessingState state = new ProcessingState();
        
        processChildren(supElement, base, superscript, state, callback);
        
        // 处理基础文本
        String baseText = base.toString().trim();
        String supText = superscript.toString().trim();
        
        appendFormula(formula, baseText, supText, state);
    }
    
    private static class ProcessingState {
        boolean hasContent = false;
        boolean hasNestedScripts = false;
        boolean needsBrackets = false;
    }
    
    private static void processChildren(Element element, StringBuilder base, StringBuilder superscript,
                                     ProcessingState state, ProcessorCallback callback) {
        NodeList children = element.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE) continue;
            
            Element childElement = (Element) child;
            String nodeName = childElement.getNodeName();
            
            if ("m:e".equals(nodeName)) { // 处理底数部分
                callback.processElement(childElement, base);
                // 检查基础文本中是否包含上下标
                if (base.toString().contains("^") || base.toString().contains("_")) {
                    state.hasNestedScripts = true;
                }
            } else if ("m:sup".equals(nodeName)) { // 处理上标部分
                callback.processElement(childElement, superscript);
                state.hasContent = true;
                // 检查上标文本复杂度
                String supContent = superscript.toString();
                if (supContent.matches(".*[+\\-\\*/].*") || supContent.contains("\\")) {
                    state.needsBrackets = true;
                }
            }
        }
    }
    
    private static void appendFormula(StringBuilder formula, String baseText, String supText, ProcessingState state) {
        if (baseText.isEmpty()) return;
        
        // 如果基础文本中有嵌套的上下标，需要用括号括起来
        if (state.hasNestedScripts) {
            formula.append("(").append(baseText).append(")");
        } else {
            formula.append(baseText);
        }
        
        if (state.hasContent) {
            if (supText.equals("°") || supText.equals("∘")) {
                formula.append("^{\\circ}");
            } else if (state.needsBrackets || supText.length() > 1) {
                formula.append("^{").append(supText).append("}");
            } else {
                formula.append("^").append(supText);
            }
        }
    }
}
